<?php
namespace sycapi\wechat;

use sycapi\wechat\base\ErrCode;
use sycapi\wechat\src\Card;
use sycapi\wechat\src\Custom;
use sycapi\wechat\src\Device;
use sycapi\wechat\src\Extend;
use sycapi\wechat\src\Hardware;
use sycapi\wechat\src\Media;
use sycapi\wechat\src\Menu;
use sycapi\wechat\src\Message;
use sycapi\wechat\src\Oauth;
use sycapi\wechat\src\Poi;
use sycapi\wechat\src\Receive;
use sycapi\wechat\src\Script;
use sycapi\wechat\src\User;
use sycapi\wechat\support\Exception;

use think\Config;

class Wechat{
    protected static $instance;

    public function __construct(array $options = []){}

    /** 调用信息发布，群发，模板，等操作 */
    public static function message($options=[]){
        $options =self::getOptions($options);
        $sn = self::getSn($options);
        if(isset(self::$instance[$sn]["message"])){
            return self::$instance[$sn]["message"];
        }else{
            return self::$instance[$sn]["message"] = new Message($options);
        }
    }

    /** 获取设备相关的操作，包括摇一摇  */
    public static function device($options=[]){
        $options =self::getOptions($options);
        $sn = self::getSn($options);
        if(isset(self::$instance[$sn]["device"])){
            return self::$instance[$sn]["device"];
        }else{
            return self::$instance[$sn]["device"] = new Device($options);
        }
    }

    /** 接收并回复消息 */
    public static function receive($options=[]){
        $options =self::getOptions($options);
        $sn = self::getSn($options);
        if(isset(self::$instance[$sn]["receive"])){
            return self::$instance[$sn]["receive"];
        }else{
            return self::$instance[$sn]["receive"] = new Receive($options);
        }
    }

    /** 菜单相关 */
    public static function menu($options=[]){
        $options =self::getOptions($options);
        $sn = self::getSn($options);
        if(isset(self::$instance[$sn]["menu"])){
            return self::$instance[$sn]["menu"];
        }else{
            return self::$instance[$sn]["menu"] = new Menu($options);
        }
    }

    /** JavaScript脚本相关 */
    public static function script($options=[]){
        $options =self::getOptions($options);
        $sn = self::getSn($options);
        if(isset(self::$instance[$sn]["script"])){
            return self::$instance[$sn]["script"];
        }else{
            return self::$instance[$sn]["script"] = new Script($options);
        }
    }

    /** 用户信息相关 */
    public static function user($options=[]){
        $options =self::getOptions($options);
        $sn = self::getSn($options);
        if(isset(self::$instance[$sn]["user"])){
            return self::$instance[$sn]["user"];
        }else{
            return self::$instance[$sn]["user"] = new User($options);
        }
    }

    /** 获取卡券相关 */
    public static function card($options=[]){
        $options =self::getOptions($options);
        $sn = self::getSn($options);
        if(isset(self::$instance[$sn]["card"])){
            return self::$instance[$sn]["card"];
        }else{
            return self::$instance[$sn]["card"] = new Card($options);
        }
    }

    /** 设备相关 */
    public static function hardware($options=[]){
        $options =self::getOptions($options);
        $sn = self::getSn($options);
        if(isset(self::$instance[$sn]["hardware"])){
            return self::$instance[$sn]["hardware"];
        }else{
            return self::$instance[$sn]["hardware"] = new Hardware($options);
        }
    }

    /** 统计数据相关 */
    public static function extend($options=[]){
        $options =self::getOptions($options);
        $sn = self::getSn($options);
        if(isset(self::$instance[$sn]["extend"])){
            return self::$instance[$sn]["extend"];
        }else{
            return self::$instance[$sn]["extend"] = new Extend($options);
        }
    }

    /** 客服相关 */
    public static function custom($options=[]){
        $options =self::getOptions($options);
        $sn = self::getSn($options);
        if(isset(self::$instance[$sn]["custom"])){
            return self::$instance[$sn]["custom"];
        }else{
            return self::$instance[$sn]["custom"] = new Custom($options);
        }
    }

    /** 授权相关 */
    public static function oauth($options=[]){
        $options =self::getOptions($options);
        $sn = self::getSn($options);
        if(isset(self::$instance[$sn]["oauth"])){
            return self::$instance[$sn]["oauth"];
        }else{
            return self::$instance[$sn]["oauth"] = new Oauth($options);
        }
    }

    /** 媒体相关 */
    public static function media($options=[]){
        $options =self::getOptions($options);
        $sn = self::getSn($options);
        if(isset(self::$instance[$sn]["media"])){
            return self::$instance[$sn]["media"];
        }else{
            return self::$instance[$sn]["media"] = new Media($options);
        }
    }

    /** 门店相关 */
    public static function poi($options=[]){
        $options =self::getOptions($options);
        $sn = self::getSn($options);
        if(isset(self::$instance[$sn]["poi"])){
            return self::$instance[$sn]["poi"];
        }else{
            return self::$instance[$sn]["poi"] = new Poi($options);
        }
    }

    /** 获取SN */
    protected static function getSn(array $options = []){
        // $options =self::getOptions($options);
        return md5("{$options["appid"]}{$options["appsecret"]}");
    }

    /** 获取配置 */
    protected static function getOptions( $options = []){
        if (empty($options)&& !empty(Config::get("wechat.default_options_name"))){
            $options = Config::get("wechat")[Config::get("wechat.default_options_name")];
        }elseif(is_string($options)&&!empty( Config::get("wechat.$options"))){
            $options = Config::get("wechat.$options");
        }
        if (empty($options)) {
            $error[]="获取Token参数缺失";
            // throw new Exception("微信配置参数不存在");
            return "获取Token参数缺失";
        }elseif(isset($options["appid"])&&isset($options["appsecret"])){
            return $options ;
        }else{
            // throw new Exception("微信配置参数不完整");
            return "微信配置参数不完整" ;
        }
    }

    /** 获取错误文本 */
    static public function getErrText($code){
        return ErrCode::getErrText($code);
    }

}